var layer = layui.layer;
new Vue({
    el: "#container",
    data() {
        return {
            showTaskTable:false,
            // 站点列表
            stationList:[],
            // 角色列表
            roleList:[],
            // 功能类型
            funTypeList:[
                {value:1,label:"微信订单"},
                /*{value:2,label:"物资审核"},*/
            ],
            // 审核等级
            examineLevelList:[
                {value:1,label: "N+1"},
                {value:2,label: "N+2"},
                {value:3,label: "N+3"},
                {value:4,label: "N+4"},
            ],
            // 数据列表
            dataList:[],
            serarchFrom:{
                roleId:"",
                offset:0,
                // 每页显示数量
                limit:15,
                total:0
            },
            addFrom : {
                // 站点id
                orgId:"",
                // 站点名
                orgName:"",
                // 角色id
                roleId:"",
                // 角色名
                roleName:"",
                // 数据类型
                type:"",
                typeLabel:"",
                // 审核等级
                level:"",
            },

        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.initOrigin();
        that.initRoleList();
        that.loadData();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {

    },
    methods: {
        /**初始化站点**/
        initOrigin(){
            var that = this;
            $.ajax({
                type: "post",
                url: '/sys_origin/query/user/origin',
                async: true,
                dataType: 'json',
                timeout: 30000,
                data: {type: 2, status: 0},
                success: function (res) {
                    if (res.code == 0) {
                        that.stationList = res.data;
                    } else {
                        // 您未关联站点,请联系管理人员
                    }
                },
                error: function (res) {
                    // wapAlert(res.msg);
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    if (status == 'timeout') {
                        wapAlert('网络链接超时,请稍后再试');
                        return;
                    }
                }
            });
        },
        // 初始化角色列表
        initRoleList:function(){
            var that = this;
            $.ajax({
                type: "post",
                url: '/console/role/pos/list',
                async: true,
                dataType: 'json',
                timeout: 30000,
                success: function (res) {
                    if (res.code == 0) {
                        that.roleList = res.data;
                    } else {
                        // 您未关联站点,请联系管理人员
                    }
                },
                error: function (res) {
                    // wapAlert(res.msg);
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    if (status == 'timeout') {
                        wapAlert('网络链接超时,请稍后再试');
                        return;
                    }
                }
            });
        },
        /**
         * 修改页码
         * @param val
         */
        handleCurrentChange(val) {
            // 计算起始页码
            var that = this;
            let limit = that.serarchFrom.limit;
            let offset = (val - 1) * limit;
            that.serarchFrom.offset = offset;
            that.loadData();
        },
        // 新增任务
        addTask:function () {
            this.showTaskTable = true;
        },
        // 关闭任务窗口
        closeTaskTable:function () {
            this.showTaskTable = false;
            this.clearForm();
        },
        // 保存任务
        submitForm:function(formName) {
            var that = this;
            that.saveTask();
        },
        // 保存任务
        saveTask:function () {
            var that = this;
            var loading = layer.load(1);
            var data = that.addFrom;
            $.ajax({
                type: "put",
                url: "/role/examine/level/save",
                async: true,
                dataType: 'json',
                timeout: 30000,
                data: data,
                success: function(res) {
                    layer.close(loading);
                    if(res.code == 0) {
                        layer.msg("新建角色审核成功");
                        that.showTaskTable = false;
                        that.clearForm();
                        that.loadData();
                    } else {
                        layer.msg(res.msg);
                        return;
                    }
                },
                error: function(res) {
                    layer.msg("请求失败，请稍后再试！");
                    layer.close(loading);
                    return;
                },
                complete: function(XMLHttpRequest, status) {

                }
            });
        },
        // 加载任务数据
        loadData:function(){
            var that = this;
            $.ajax({
                type: "post",
                url: "/role/examine/level/list",
                async: true,
                dataType: 'json',
                timeout: 50000,
                data: that.serarchFrom,
                success: function(res) {
                    if(res.code == 0) {
                        that.dataList = res.data.records;
                        that.serarchFrom.total = res.data.total;
                    } else {
                        layer.msg(res.msg);
                        return;
                    }
                },
                error: function(res) {
                    layer.msg("请求失败，请稍后再试！");
                    return;
                },

            });
        },
        del(id){
            var that = this;
            var loadIndex = layer.load();
            $.ajax({
                type: "delete",
                url: "/role/examine/level/delete/" + id,
                async: true,
                dataType: 'json',
                timeout: 50000,
                data: that.serarchFrom,
                success: function(res) {
                    layer.close(loadIndex);
                    if(res.code == 0){
                        that.loadData();
                    }else {
                        layer.msg("删除失败：" + res.msg);
                    }
                },
                error: function(res) {
                    layer.close(loadIndex);
                    layer.msg("请求失败，请稍后再试！");
                    return;
                },

            });
        },
        // 清空表单数据
        clearForm(){
            var that = this;
            // 清楚表单属性
            for(var key in that.addFrom){
                delete that.addFrom[key];
            }
        },
        // 更新任务
        updateTaskForm:function (id) {

        },
        // 站点修改
        stationChange:function(data){
            var that = this;
            const { value, label } = data;
            that.addFrom.orgId = value;
            that.addFrom.orgName = label;
        },
        // 角色修改
        roleChange:function(data){
            var that = this;
            const { value, label } = data;
            that.addFrom.roleId = value;
            that.addFrom.roleName = label;
        },
        // 功能修改
        typeChange:function(data){
            var that = this;
            const { value, label } = data;
            that.addFrom.type = value;
            that.addFrom.typeLabel = label;
        },
    }
})
